<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>cache-storage</title>
</head>
<body>
<script>
    'use strict'
    ;(function() {
            var ls=window.localStorage
            function oops() {
                return console.warn('your browser is not supported localStorage api')
            }
            function getItem(key){
                var data=ls.getItem(key)
                data = JSON.parse(data) || {}
                if (data.time === 0) {
                    return data.value
                } else if (Date.now() > data.time) {
                    ls.removeItem(key)
                    return ''
                } else {
                    return typeof data.value !== 'undefined' ? data.value : ''
                }
            }
            function setItem(key,value,time){
                if(typeof key === 'undefined') {return}
                var data = {
                    time: time ? Date.now() + time : 0,
                    value : value
                }
                data = JSON.stringify(data)
                try {
                    ls.setItem(key, data)
                } catch(e){
                    ls.clear()
                    ls.setItem(key, data)
                }
            }
            function removeItem(){
                ls.removeItem(key)
            }
            function clear(){
                ls.clear()
            }
            window.cacheStorage={
                getItem: ls ? getItem : oops,
                setItem: ls ? setItem : oops,
                removeItem: ls ? removeItem : oops,
                clear : ls ? clear :clear
            }
        })()
</script>
</body>
</html>